package st.business.tepth.business.taskallocation.detail;

import com.alibaba.fastjson.TypeReference;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.disposables.Disposable;
import st.business.tepth.base.OnDataConvertListener;
import st.business.tepth.business.bean.post.TaskAllocationDetail;
import st.business.tepth.config.ItemFields;
import st.business.tepth.config.Url;
import st.shadow.library.core.net.callback.IError;
import st.shadow.library.core.net.callback.ISuccess;
import st.shadow.library.core.net.data.ObjConverter;
import st.shadow.library.core.net.rx.RxRestClient;
import st.shadow.library.ui.recycler.MultipleItemEntity;

/**
 * Author: shadow
 * Email:  1025431817@qq.com
 * Time:   2019-02-26 11:59
 */

public class AllocationDetailModel implements AllocationDetailContract.Model {

    @Override
    public Disposable allocationTasks(String token, int repairId, final String repairTaskList,
                                      final OnDataConvertListener<String> listener) {
        return RxRestClient.builder()
                .url(Url.AllocationTask)
                .params("token", token)
                .params("repairId", repairId)
                .params("repairTaskList", repairTaskList)
                .success(new ISuccess() {
                    @Override
                    public void onSuccess(String response) {
                        listener.convert(response);
                    }
                })
                .error(new IError() {
                    @Override
                    public void onError(int code, String msg) {
                        listener.error(code, msg);
                    }
                })
                .build()
                .post();
    }

    @Override
    public Disposable getTaskList(String token, int repairId,
                                  final OnDataConvertListener<List<MultipleItemEntity>> listener) {
        return RxRestClient.builder()
                .url(Url.GetAllocationDetailList)
                .params("token", token)
                .params("repairId", repairId)
                .success(new ISuccess() {
                    @Override
                    public void onSuccess(String response) {
                        listener.convert(convertToTaskList(response));
                    }
                })
                .error(new IError() {
                    @Override
                    public void onError(int code, String msg) {
                        listener.error(code, msg);
                    }
                })
                .build()
                .post();
    }

    @Override
    public Disposable cancelAllocate(String token, int repairTaskId, int userId,
                                     final OnDataConvertListener<String> listener) {
        return RxRestClient.builder()
                .url(Url.CancelAllocate)
                .params("token", token)
                .params("repairTaskId", repairTaskId)
                .params("userId", userId)
                .success(new ISuccess() {
                    @Override
                    public void onSuccess(String response) {
                        listener.convert(response);
                    }
                })
                .error(new IError() {
                    @Override
                    public void onError(int code, String msg) {
                        listener.error(code, msg);
                    }
                })
                .build()
                .post();
    }

    private List<MultipleItemEntity> convertToTaskList(String response) {
        TaskAllocationDetail taskAllocationDetail = new ObjConverter<TaskAllocationDetail>().convert(response
                , new TypeReference<TaskAllocationDetail>() {
                });
        List<MultipleItemEntity> list = new ArrayList<>();
        if (taskAllocationDetail != null) {
            List<TaskAllocationDetail.Task> repairTaskList =
                    taskAllocationDetail.getRepairTaskList();
            if (repairTaskList != null) {
                for (TaskAllocationDetail.Task task : repairTaskList) {
                    List<Integer> personIdList = new ArrayList<>();
                    list.add(MultipleItemEntity.builder()
                            .setItemType(AllocationDetailAdapter.TYPE_0)
                            .setField("personIdList", personIdList)
                            .setField("id",task.getId())
                            .setField(ItemFields.ITEM_CONSTANT, task.getVehicle_series_name())
                            .build());
                    List<TaskAllocationDetail.Person> userList =
                            task.getUserList();
                    if (userList != null) {
                        for (TaskAllocationDetail.Person person : userList) {
                            list.add(MultipleItemEntity.builder()
                                    .setItemType(AllocationDetailAdapter.TYPE_1)
                                    .setField(ItemFields.ITEM_OBJ, person)
                                    .setField("checked", person.isSelected())
                                    .setField("personId", person.getId())
                                    .setField("cancelable", person.isSelected()
                                            && !("竣检".equals(task.getVehicle_series_name()))
                                            && !("过检".equals(task.getVehicle_series_name())))
                                    .setField("isFinish", "竣检".equals(task.getVehicle_series_name()))
                                    .setField("classId", task.getId())
                                    .build());
                            personIdList.add(person.getId());
                        }
                    }
                    list.add(MultipleItemEntity.builder()
                            .setItemType(AllocationDetailAdapter.TYPE_2)
                            .setField(ItemFields.ITEM_CONSTANT, task.getPhenomenon_name())
                            .setField("classId", task.getId())
                            .build());
                }
            }
        }
        return list;
    }
}
